//页面加载完成
$(function () {

    var  attendance_datagrid =  $("#attendance_datagrid");

    //初始员工列表界面
    attendance_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: '/attendance/list',
        rownumbers: true,
        pagination: true,
        singleSelect:true,
        columns: [[
            {field: 'id', hidden:true},
            {field: 'currentDate', title: '日期',width: 100},
            {field: 'employee', title: '员工姓名',width: 100,formatter:employeeFormatter},
            {field: 'department', title: '部门',width: 100,formatter:dpeartmentFormatter},
            {field: 'beginTime', title: '上班时间',width: 100},
            {field: 'endTime', title: '下班时间',width: 100},
            {field: 'state', title: '状态',width: 100,formatter:stateFormatter}
        ]],
        toolbar: "#attendance_toolbar"
    });
    //将方法绑定到对象中
    var cmdObj = {
        searchForAtt:function () {
            var beginDate = $("#beginDate").val();
            var endDate = $("#endDate").val();
            var keyword= $("#keyword").val();
            var deptId= $("#deptId").val();
            //再次加载数据
            attendance_datagrid.datagrid("load",{deptId:deptId,keyword:keyword,beginDate:beginDate,endDate:endDate});
        }
    }
    //使用js来绑定事件  使用了属性过滤器来完成该操作
    $("a[data-cmd]").click(function(){
        var cmd = $(this).data("cmd");
        //调用方法
        cmdObj[cmd]();
    });
    //end
})
//部门格式化操作
function dpeartmentFormatter(value, row, index) {

    return value ? value.name : "";

}
//员工格式化操作
function employeeFormatter(value, row, index) {

    return value ? value.userName : "";

}

//状态格式化回显
function stateFormatter(value, row, index) {
    if (value == 0) {
        return "<span style='color: #00ee00'>"+"正常"+"</span>";
    } else if (value == 1) {
        return "<span style='color: red'>" + "迟到" + "</span>";
    } else if (value == 2){
        return "<span style='color: yellow'>"+"早退"+"</span>";
    }
}

